home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_mysql.idb / usr / freeware / bin / safe_mysqld.z / safe_mysqld
Encoding:
Text File  |  2002-10-07  |  8.2 KB  |  277 lines

  1. #!/bin/sh
  2. # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
  3. # This file is public domain and comes with NO WARRANTY of any kind
  4. #
  5. # scripts to start the MySQL daemon and restart it if it dies unexpectedly
  6. #
  7. # This should be executed in the MySQL base directory if you are using a
  8. # binary installation that has other paths than you are using.
  9. #
  10. # mysql.server works by first doing a cd to the base directory and from there
  11. # executing safe_mysqld
  12.  
  13. trap '' 1 2 3 15            # we shouldn't let anyone kill us
  14.  
  15. defaults=
  16. case "$1" in
  17.     --no-defaults|--defaults-file=*|--defaults-extra-file=*)
  18.       defaults="$1"; shift
  19.       ;;
  20. esac
  21.  
  22. parse_arguments() {
  23.   # We only need to pass arguments through to the server if we don't
  24.   # handle them here.  So, we collect unrecognized options (passed on
  25.   # the command line) into the args variable.
  26.   pick_args=$1; shift
  27.  
  28.   for arg do
  29.     case "$arg" in
  30.       # these get passed explicitly to mysqld
  31.       --basedir=*) MY_BASEDIR_VERSION=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  32.       --datadir=*) DATADIR=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  33.       --pid-file=*) pid_file=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  34.       --user=*)    user=`echo "$arg" | sed -e "s;--[^=]*=;;"` ; SET_USER=1 ;;
  35.  
  36.       # these two might have been set in a [safe_mysqld] section of my.cnf
  37.       # they get passed via environment variables to safe_mysqld
  38.       --socket=*)  MYSQL_UNIX_PORT=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  39.       --port=*)    MYSQL_TCP_PORT=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  40.  
  41.       # safe_mysqld-specific options
  42.       --ledir=*)   ledir=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  43.       --err-log=*) err_log=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  44.       # QQ The --open-files should be removed
  45.       --open-files=*) open_files=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  46.       --open-files-limit=*) open_files=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  47.       --core-file-size=*) core_file_size=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  48.       --timezone=*) TZ=`echo "$arg" | sed -e "s;--[^=]*=;;"` ; export TZ; ;;
  49.       --mysqld=*)   MYSQLD=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  50.       --mysqld-version=*)
  51.     tmp=`echo "$arg" | sed -e "s;--[^=]*=;;"`
  52.     if test -n "$tmp"
  53.     then
  54.       MYSQLD="mysqld-$tmp"
  55.     else
  56.       MYSQLD="mysqld"
  57.     fi
  58.     ;;
  59.       *)
  60.         if test $pick_args -eq 1
  61.         then
  62.           # This sed command makes sure that any special chars are quoted,
  63.           # so the arg gets passed exactly to the server.
  64.           args="$args "`echo "$arg" | sed -e 's,\([^a-zA-Z0-9_.-]\),\\\\\1,g'`
  65.         fi
  66.         ;;
  67.     esac
  68.   done
  69. }
  70.  
  71. MY_PWD=`pwd`
  72. # Check if we are starting this relative (for the binary release)
  73. if test -d $MY_PWD/data/mysql -a -f ./share/mysql/english/errmsg.sys -a \
  74.  -x ./bin/mysqld
  75. then
  76.   MY_BASEDIR_VERSION=$MY_PWD        # Where bin, share and data are
  77.   ledir=$MY_BASEDIR_VERSION/bin        # Where mysqld is
  78.   DATADIR=$MY_BASEDIR_VERSION/data
  79.   if test -z "$defaults"
  80.   then
  81.     defaults="--defaults-extra-file=$MY_BASEDIR_VERSION/data/my.cnf"
  82.   fi
  83. # Check if this is a 'moved install directory'
  84. elif test -f ./var/mysql/db.frm -a -f ./share/mysql/english/errmsg.sys -a \
  85.  -x ./libexec/mysqld
  86. then
  87.   MY_BASEDIR_VERSION=$MY_PWD        # Where libexec, share and var are
  88.   ledir=$MY_BASEDIR_VERSION/libexec    # Where mysqld is
  89.   DATADIR=$MY_BASEDIR_VERSION/var
  90. else
  91.   MY_BASEDIR_VERSION=/usr/freeware
  92.   DATADIR=/var/spool/mysql
  93.   ledir=/usr/freeware/sbin
  94. fi
  95.  
  96. MYSQL_UNIX_PORT=${MYSQL_UNIX_PORT:-/tmp/mysql.sock}
  97. MYSQL_TCP_PORT=${MYSQL_TCP_PORT:-3306}
  98. user=mysql
  99.  
  100. # Use the mysqld-max binary by default if the user doesn't specify a binary
  101. if test -x $ledir/mysqld-max
  102. then
  103.   MYSQLD=mysqld-max
  104. else
  105.   MYSQLD=mysqld
  106. fi
  107.  
  108. # these rely on $DATADIR by default, so we'll set them later on
  109. pid_file=
  110. err_log=
  111. SET_USER=0
  112.  
  113. # Get first arguments from the my.cnf file, groups [mysqld] and [safe_mysqld]
  114. # and then merge with the command line arguments
  115. if test -x ./bin/my_print_defaults
  116. then
  117.   print_defaults="./bin/my_print_defaults"
  118. elif test -x /usr/freeware/bin/my_print_defaults
  119. then
  120.   print_defaults="/usr/freeware/bin/my_print_defaults"
  121. elif test -x /usr/freeware/bin/mysql_print_defaults
  122. then
  123.   print_defaults="/usr/freeware/bin/mysql_print_defaults"
  124. else
  125.   print_defaults="my_print_defaults"
  126. fi
  127.  
  128. args=
  129. parse_arguments 0 `$print_defaults $defaults mysqld server safe_mysqld`
  130. parse_arguments 1 "$@"
  131.  
  132. if test ! -x $ledir/$MYSQLD
  133. then
  134.   echo "The file $ledir/$MYSQLD doesn't exist or is not executable"
  135.   echo "Please do a cd to the mysql installation directory and restart"
  136.   echo "this script from there as follows:"
  137.   echo "./bin/safe_mysqld".
  138.   exit 1
  139. fi
  140.  
  141. if test -z "$pid_file"
  142. then
  143.   pid_file=$DATADIR/`/usr/bsd/hostname`.pid
  144. else
  145.   case "$pid_file" in
  146.     /* ) ;;
  147.     * )  pid_file="$DATADIR/$pid_file" ;;
  148.   esac
  149. fi
  150. test -z "$err_log"  && err_log=$DATADIR/`/usr/bsd/hostname`.err
  151.  
  152. export MYSQL_UNIX_PORT
  153. export MYSQL_TCP_PORT
  154.  
  155.  
  156. NOHUP_NICENESS="nohup"
  157. if test -w /
  158. then
  159.   NOHUP_NICENESS=`nohup nice 2>&1`
  160.  if test $? -eq 0 && test x"$NOHUP_NICENESS" != x0 && nice --1 echo foo > /dev/null 2>&1
  161.  then
  162.     NOHUP_NICENESS="nice --$NOHUP_NICENESS nohup"
  163.   else
  164.     NOHUP_NICENESS="nohup"
  165.   fi
  166. fi
  167.  
  168. USER_OPTION=""
  169. if test -w /
  170. then
  171.   if test "$user" != "root" -o $SET_USER = 1
  172.   then
  173.     USER_OPTION="--user=$user"
  174.   fi
  175.   # If we are root, change the err log to the right user.
  176.   touch $err_log; chown $user $err_log
  177.   if test -n "$open_files"
  178.   then
  179.     ulimit -n $open_files
  180.   fi
  181.   if test -n "$core_file_size"
  182.   then
  183.     ulimit -c $core_file_size
  184.   fi
  185. fi
  186.  
  187. #
  188. # If there exists an old pid file, check if the daemon is already running
  189. # Note: The switches to 'ps' may depend on your operating system
  190. if test -f $pid_file
  191. then
  192.   PID=`cat $pid_file`
  193.   if /usr/bin/kill -0 $PID > /dev/null 2> /dev/null
  194.   then
  195.     if /sbin/ps -ef | grep mysqld | grep " $PID " > /dev/null
  196.     then    # The pid contains a mysqld process
  197.       echo "A mysqld process already exists"
  198.       echo "A mysqld process already exists at " `date` >> $err_log
  199.       exit 1
  200.     fi
  201.   fi
  202.   rm -f $pid_file
  203.   if test -f $pid_file
  204.   then
  205.     echo "Fatal error: Can't remove the pid file: $pid_file"
  206.     echo "Fatal error: Can't remove the pid file: $pid_file at " `date` >> $err_log
  207.     echo "Please remove it manually and start $0 again"
  208.     echo "mysqld daemon not started"
  209.     exit 1
  210.   fi
  211. fi
  212.  
  213. #
  214. # Uncomment the following lines if you want all tables to be automaticly
  215. # checked and repaired at start
  216. #
  217. # echo "Checking tables in $DATADIR"
  218. # $MY_BASEDIR_VERSION/bin/myisamchk --silent --force --fast --medium-check -O key_buffer=64M -O sort_buffer=64M $DATADIR/*/*.MYI
  219. # $MY_BASEDIR_VERSION/bin/isamchk --silent --force -O sort_buffer=64M $DATADIR/*/*.ISM
  220.  
  221. echo "Starting $MYSQLD daemon with databases from $DATADIR"
  222.  
  223. # Does this work on all systems?
  224. #if type ulimit | grep "shell builtin" > /dev/null
  225. #then
  226. #  ulimit -n 256 > /dev/null 2>&1        # Fix for BSD and FreeBSD systems
  227. #fi
  228.  
  229. echo "`date +'%y%m%d %H:%M:%S  mysqld started'`" >> $err_log
  230. while true
  231. do
  232.   rm -f $MYSQL_UNIX_PORT $pid_file    # Some extra safety
  233.   if test -z "$args"
  234.   then
  235.     $NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file  >> $err_log 2>&1
  236.   else
  237.     eval "$NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file  $args >> $err_log 2>&1"
  238.   fi
  239.   if test ! -f $pid_file        # This is removed if normal shutdown
  240.   then
  241.     break
  242.   fi
  243.  
  244.   if false
  245.   then
  246.     # Test if one process was hanging.
  247.     # This is only a fix for Linux (running as base 3 mysqld processes)
  248.     # but should work for the rest of the servers.
  249.     # The only thing is ps x => redhat 5 gives warnings when using ps -x.
  250.     # kill -9 is used or the process won't react on the kill.
  251.     numofproces=`ps xa | grep -v "grep" | grep -c $ledir/$MYSQLD`
  252.     echo -e "\nNumber of processes running now: $numofproces" | tee -a $err_log
  253.     I=1
  254.     while test "$I" -le "$numofproces"
  255.     do 
  256.       PROC=`ps xa | grep $ledir/$MYSQLD | grep -v "grep" | sed -n '$p'` 
  257.     for T in $PROC
  258.     do
  259.       break
  260.     done
  261.     #    echo "TEST $I - $T **"
  262.     if kill -9 $T
  263.     then
  264.       echo "$MYSQLD process hanging, pid $T - killed" | tee -a $err_log
  265.     else 
  266.       break
  267.     fi
  268.     I=`expr $I + 1`
  269.     done
  270.   fi
  271.  
  272.   echo "`date +'%y%m%d %H:%M:%S  mysqld restarted'`" | tee -a $err_log
  273. done
  274.  
  275. echo "`date +'%y%m%d %H:%M:%S  mysqld ended'`" | tee -a $err_log
  276. echo "" | tee -a $err_log
  277.